<template>
    <div class="app-header">
        <div class="container">
            <!-- logo -->
            <h1 class="logo">
                <router-link to="/"></router-link>
            </h1>
            <!-- 导航区域 -->
            <ul class="app-header-nav">
                <li class="home"><router-link to="/">首页</router-link></li>
                <li class="hh" v-for="item in list" :key="item.id" @mouseover="$set(submenuOpenStatus, item.id, true)"
                    @mouseleave="$set(submenuOpenStatus, item.id, false)">
                    <router-link to="/">{{ item.name }}</router-link>
                    <div :class="{ 'layer': true, 'open': submenuOpenStatus[item.id] }">
                        <ul class="aiai">
                            <li v-for="i in item.children" :key="i.id">
                                <router-link to="/">
                                <img :src="i.picture" alt="">
                                <p>{{ i.name }}</p>
                                </router-link>
                            </li>
                        </ul>
                    </div>
                </li>
            </ul>
            <!-- 搜索和购物车 -->
            <div class="search">
                <i class="el-icon-search"></i>
                <input class="inp-search" type="text" placeholder="搜一搜" />
            </div>
            <div class="cart">
                <a href="/">
                    <i class="el-icon-shopping-cart-2"></i>
                    <em>0</em>
                </a>
            </div>
        </div>
    </div>
</template>

<script>
import { getCategory } from '@/request/api/myc/myc';
export default {
    data() {
        return {
            list: [],
            submenuOpenStatus: {}
        };
    },
    mounted() {
        this.Category();

        // 初始化submenuOpenStatus对象
        this.list.forEach(item => {
            this.$set(this.submenuOpenStatus, item.id, false);
        });
    },
    methods: {
        async Category() {
            const res = await getCategory();
            console.log("新鲜好物", res.data.result);
            this.list = res.data.result
        }
    }
};
</script>

<style lang="scss" scoped>
.app-header {
    background-color: #ffff;
}

.container {
    display: flex;
    align-items: center;

    .logo {
        width: 200px;
        height: 134px;
        background-image: url("../assets/images/logo.png");
        background-size: 160px;
        background-repeat: no-repeat;
        background-position: 20px 27px;
    }

    .app-header-nav {
        display: flex;
        width: 820px;
        justify-content: space-evenly;

        .home {
            a {

                // 对于频繁使用的东西 都可以定义变量
                &:hover {
                    color: #27ba9b;
                    border-bottom: 1px solid #27ba9b;
                }
            }
        }

        .hh {
            a {

                // 对于频繁使用的东西 都可以定义变量
                &:hover {
                    color: #27ba9b;
                    border-bottom: 1px solid #27ba9b;
                }

            }

            .layer {
                width: 1240px;
                background-color: #fff;
                position: absolute;
                left: 0;
                top: 100px;
                height: 0;
                overflow: hidden;
                opacity: 0;
                box-shadow: 0 0 5px #ccc;
                transition: all .2s .1s;
                z-index: 500;
                .aiai {
                    display: flex;
                    flex-wrap: wrap;
                    padding: 0 70px;
                    align-items: center;
                    height: 132px;

                    li {
                        width: 110px;
                        text-align: center;

                        a {


                            img {
                                width: 60px;
                                height: 60px;
                            }

                            p {
                                padding-top: 10px;
                            }
                        }
                    }
                }
            }

            .layer.open {
                height: 132px;
                opacity: 1;
            }
        }
    }

    .search {
        width: 170px;
        height: 32px;
        background-color: white;
        border-bottom: 2px solid #e7e7e7;

        .sou {
            width: 18px;
            height: 18px;
            display: inline-block;
            margin-top: 4px;
            margin-left: 4px;
        }

        .inp-search {
            width: 140px;
            height: 28px;
        }
    }

    .cart {
        position: relative;

        .icon-gouwuche {
            font-size: 22px;
            margin-left: 10px;
        }

        em {
            font-style: normal;
            position: absolute;
            right: -10px;
            top: 0;
            padding: 1px 4px;
            line-height: 1;
            background: #e26237;
            color: #fff;
            font-size: 12px;
            border-radius: 10px;
            font-family: Arial;
        }
    }
}</style>